package cn.sxt.users.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Repository;

import cn.sxt.common.dao.impl.BaseDaoImpl;
import cn.sxt.common.util.ConstatFinalUtil;
import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.users.dao.IARoleDao;
import cn.sxt.users.pojo.ARole;

/**
 * 角色Dao的实现类
 * @author wangshSxt
 *
 */
@Repository("roleDao")
public class ARoleDaoImpl extends BaseDaoImpl<ARole> implements IARoleDao
{
	@Override
	public ARole findOneDao(Map<String, Object> condMap)
	{
		StringBuffer hql = new StringBuffer();
		/* Hibernate要求的查询条件 */
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		/* 只关心HQL和查询条件 */
		
		hql.append(" from ARole ad where 1 = 1 ");
		
		/* 按照ID查询 */
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			hql.append(" and ad.id = :id ");
			paramsMap.put("id", Integer.valueOf(condMap.get("id") + ""));
		}
		
		/* 
		 * 总结:查询条件不一定和HQL要求的Map一致
		 * HQL:and id in (:id1,:id2:id3,:id4)
		 * 查询条件:List [1,2,3,4],map(key:id1,value=2) 
		 * */
		
		/* 排序 */
		if("createASC".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			hql.append(" order by ad.createTime asc ");
		}else
		{
			hql.append(" order by ad.updateTime desc ");
		}
		
		/* 调用父类的查询单条的方法 */
		return this.findSinge(hql.toString(), paramsMap);
	}

	@Override
	public List<ARole> findCondListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		StringBuffer hqlSb = new StringBuffer() ; 
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		
		hqlSb.append("from ARole ad where 1 = 1 ");
		
		/* 按照关键字搜索 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			hqlSb.append(" and (ad.email like :keyword or ad.trueName like :keyword or ad.phone like :keyword ) ");
			paramsMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/* 按照状态搜索 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			hqlSb.append(" and ad.status = :status ");
			paramsMap.put("status", Byte.valueOf(condMap.get("status") + ""));
		}
		
		/* 按照更新时间搜索 */
		if(condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			hqlSb.append(" and ad.updateTime >= :stDate and ad.updateTime < :edDate ");
			paramsMap.put("stDate", condMap.get("stDate"));
			paramsMap.put("edDate", condMap.get("edDate"));
		}
		
		/* 排序 */
		if("createASC".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			hqlSb.append(" order by ad.createTime asc ");
		}else
		{
			hqlSb.append(" order by ad.updateTime desc ");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			return this.findByPage(pageInfoUtil, hqlSb.toString(), paramsMap);
		}
		return this.find(hqlSb.toString(), paramsMap);
	}

}
